define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher/teacher/index' + location.search,
                    add_url: 'teacher/teacher/add',
                    edit_url: 'teacher/teacher/edit',
                    del_url: 'teacher/teacher/del',
                    multi_url: '',
                    import_url: '',
                    table: 'teacher',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'sex', title: __('Sex'), operate: 'LIKE', searchable:false},
                        {field: 'age', title: __('Age'), operate: 'LIKE'},
                        {field: 'classid', title: __('Class Id'), operate: 'LIKE', formatter: Controller.api.formatter.classid, operate: 'LIKE',searchable:false},
                        {field: 'tags', title: __('Tags'), operate: 'LIKE', formatter: Controller.api.formatter.tags, operate: 'LIKE',searchable:false},
                        {field: 'education', title: __('Education'), operate: 'LIKE'},
                        {field: 'field', title: __('Field'), operate: 'LIKE'},
                        {field: 'grade', title: __('Grade'), formatter: Controller.api.formatter.grade, operate: 'LIKE',searchable:false},
                        {field: 'headimg', title: __('Headimg'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false, searchable:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, searchable:false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, searchable:false},
                        {field: 'is_live', title: __('Is Live'), operate: 'LIKE', formatter: Controller.api.formatter.islive, operate: 'LIKE',searchable:false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {"0": __('Normal'), "1": __('Disable'), "2": __('Complaint')}, custom: {"0":'success', "1":'danger', "2":'disabled'}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: { //渲染的方法
                classid: function(value, row, index) {
                    var classid_html = "<div style='max-width: 170px;display: flex;flex-wrap: wrap;'>";
                    $.each(row.classid, function (i, j) {
                        if(j.id != undefined){
                            classid_html += '<span class="label label-danger">'+j.name+'</span>&nbsp;';
                        }
                    });
                    classid_html += "</div>";
                    return classid_html;
                },
                tags: function(value, row, index) {
                    var tags_html = "<div style='max-width: 170px;display: flex;flex-wrap: wrap;'>";
                    $.each(row.tags, function (i, j) {
                        if(j.id != undefined){
                            tags_html += '<span class="label label-success">'+j.name+'</span>&nbsp;';
                        }
                    });
                    tags_html += "</div>";
                    return tags_html;
                },
                grade: function(value, row, index) {
                    var grade_html = "<div style='max-width: 170px;display: flex;flex-wrap: wrap;'>";
                    $.each(row.grade, function (i, j) {
                        if(j.id != undefined){
                            grade_html += '<span class="label label-success">'+j.grade_title+'</span>&nbsp;';
                        }
                    });
                    grade_html += "</div>";
                    return grade_html;
                },
                islive: function(value, row, index) {
                    var islive_html = "";

                    if(row.is_live > 0){
                        islive_html += '<span style="color: #000000;font-weight: bold;">咨询师</span>';
                    }

                    return islive_html;
                },
            }
        }
    };
    return Controller;
});